<?php
namespace app\common\Model;
use think\Model;

class TpCategory extends Model
{
	//获取商品的二级分类
	public function getCates(){
		$CategoryRes=$this->where(['pid'=>0])->select();
		foreach ($CategoryRes as $k => $v) {
			$CategoryRes[$k]['children']=$this->where(['pid'=>$v['id']])->select();
		}
		return $CategoryRes;
	}

	//根据顶级分类id获取二级分类和三级分类
	public function getSoncates($id){
		$CategoryRes=$this->where(['pid'=>$id])->select();//二级分类
		foreach ($CategoryRes as $k => $v) {
			$CategoryRes[$k]['children']=$this->where(['pid'=>$v['id']])->select();//三级分类
		}
		return $CategoryRes;
	}

	//获取推荐词关联信息
	public function getCatesWords($id){
		$CategoryWords=db('tp_category_words')->where(['category_id'=>$id])->select();
		return $CategoryWords;
	}

	//获取品牌关联信息及推广信息
	public function getCatesBrands($id){
		$data=array();
		$CategoryBrands=db('tp_category_brands')->where(['category_id'=>$id])->find();
		$arr=explode(',',$CategoryBrands['brands_id']);
		foreach ($arr as $k => $v) {
			$data['brands_id'][]=db('tp_brand')->where(['id'=>$v])->find();
		}
		$data['pro_img']=$CategoryBrands['pro_img'];
		$data['pro_url']=$CategoryBrands['pro_url'];
		return $data;

	}

	//获取广告关联的信息
	public function getCateAd($id){
		$CategoryAd=array();
		$TpCategory=db('tp_category_ad');
		$CategoryAd['A']=$TpCategory->where(['category_id'=>$id,'position'=>'A'])->limit(3)->order('id DESC')->select();
		$CategoryAd['B']=$TpCategory->where(['category_id'=>$id,'position'=>'B'])->find();
		$CategoryAd['C']=$TpCategory->where(['category_id'=>$id,'position'=>'C'])->find();
		return $CategoryAd;
	}

	//获取商品分类推荐位信息
	public function getCateRecpos($recposID,$pid=0){
		$CategoryRecpos=array();
		$_CategoryRecpos=db('tp_rec_item')->where(['recpos_id'=>$recposID,'value_type'=>2])->select();
		foreach ($_CategoryRecpos as $k => $v) {
			$arr=db('tp_category')->where(['id'=>$v['value_id'],'pid'=>$pid])->find();
			if($arr){
				$CategoryRecpos[]=$arr;
			}
			
		}
		return $CategoryRecpos;
	}



   
}
